<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="itemname">
        <el-input
          v-model="queryParams.itemname"
          placeholder="请输入抽检项名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="质检标准" prop="qamainguid">
        <el-select v-model="queryParams.qamainguid" placeholder="请选择质检标准" clearable>
            <el-option
              v-for="item in mainOptions"
              :key="item.guid"
              :label="item.qaname"
              :value="item.guid"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="扣杂标准" prop="qastandard">
        <el-select v-model="queryParams.qastandard" placeholder="请选择扣杂标准" clearable>
            <el-option
              v-for="item in standardOptions"
              :key="item.guid"
              :label="item.standard"
              :value="item.guid"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['quality:qaitems:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['quality:qaitems:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['quality:qaitems:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['quality:qaitems:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qaitemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="质检标准" align="center" prop="qamainname" />
      <el-table-column label="抽检项名称" align="center" prop="itemname" />
      <el-table-column label="质检项分类" align="center" prop="qastandardname" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="排序" align="center" prop="listorder" />
      <!-- <el-table-column label="操作人" align="center" prop="operatorid" />
      <el-table-column label="创建日期" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="lastupdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastupdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->


      <el-table-column label="原料薯系列" align="center" prop="rawpotatos" :formatter="rawpotatosFormat" />
      <el-table-column label="适用区域" align="center" prop="area" :formatter="areaFormat" />
      <el-table-column label="合格标准比较类型" align="center" prop="qualifiedchecktype" :formatter="qualifiedchecktypeFormat" />
      <el-table-column label="合格标准值" align="center" prop="qualifiedcheckvalue" />
      <el-table-column label="不合格比较类型" align="center" prop="unqualifiedchecktype" :formatter="unqualifiedchecktypeFormat" />
      <el-table-column label="不合格比较值" align="center" prop="unqualifiedcheckvalue" />
      <el-table-column label="奖励标准比较类型" align="center" prop="rewardchecktype" :formatter="rewardchecktypeFormat" />
      <el-table-column label="奖励标准比较值" align="center" prop="rewardcheckvalue" />
      <el-table-column label="奖励递进步长" align="center" prop="rewardcheckstep" />
      <el-table-column label="奖励值" align="center" prop="rewardvalue" />
      <el-table-column label="奖励封顶金额" align="center" prop="maxrewards" />
      <el-table-column label="惩罚标准比较类型" align="center" prop="punisheschecktype" :formatter="punisheschecktypeFormat" />
      <el-table-column label="惩罚标准比较值" align="center" prop="punishescheckvalue" />
      <el-table-column label="惩罚递进步长" align="center" prop="punishescheckstep" />
      <el-table-column label="惩罚值" align="center" prop="punishesvalue" />
      <el-table-column label="扣罚封顶金额" align="center" prop="maxpunishes" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['quality:qaitems:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['quality:qaitems:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改质检项管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="150px">
        <el-form-item label="抽检项名称" prop="itemname">
          <el-input v-model="form.itemname" placeholder="请输入抽检项名称" size="small" clearable />
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="form.status" style="width: 192px;">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="listorder">
          <el-input v-model="form.listorder" placeholder="请输入排序" size="small" clearable />
        </el-form-item>
        <el-form-item label="质检标准" prop="qamainguid">
          <el-select v-model="form.qamainguid" placeholder="请选择质检标准" size="small" clearable>
            <el-option
              v-for="item in mainOptions"
              :key="item.guid"
              :label="item.qaname"
              :value="item.guid"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="质检项分类" prop="qastandardname">
          <el-select v-model="form.qastandard" placeholder="请选择质检项分类" size="small" clearable>
            <el-option
              v-for="item in standardOptions"
              :key="item.guid"
              :label="item.standard"
              :value="item.guid"
              @click.native="getStand($event, item)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原料薯系列" prop="rawpotatos">
          <el-select v-model="form.rawpotatos" placeholder="请选择原料薯系列" size="small" clearable>
            <el-option
              v-for="dict in rawpotatosOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用区域" prop="area">
          <el-select v-model="form.area" placeholder="请选择适用区域" size="small" clearable>
            <el-option
              v-for="dict in areaOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合格标准比较类型" prop="qualifiedchecktype">
          <el-select v-model="form.qualifiedchecktype" placeholder="请选择合格标准比较类型" size="small" clearable>
            <el-option
              v-for="dict in qualifiedchecktypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合格标准值" prop="qualifiedcheckvalue">
          <el-input v-model="form.qualifiedcheckvalue" placeholder="请输入合格标准值" size="small" clearable />
        </el-form-item>
        <el-form-item label="不合格比较类型" prop="unqualifiedchecktype">
          <el-select v-model="form.unqualifiedchecktype" placeholder="请选择不合格比较类型" size="small" clearable>
            <el-option
              v-for="dict in unqualifiedchecktypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="不合格比较值" prop="unqualifiedcheckvalue">
          <el-input v-model="form.unqualifiedcheckvalue" placeholder="请输入不合格比较值" size="small" clearable />
        </el-form-item>
        <el-form-item label="奖励标准比较类型" prop="rewardchecktype">
          <el-select v-model="form.rewardchecktype" placeholder="请选择奖励标准比较类型" size="small" clearable>
            <el-option
              v-for="dict in rewardchecktypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="奖励标准比较值" prop="rewardcheckvalue">
          <el-input v-model="form.rewardcheckvalue" placeholder="请输入奖励标准比较值" size="small" clearable />
        </el-form-item>
        <el-form-item label="奖励递进步长" prop="rewardcheckstep">
          <el-input v-model="form.rewardcheckstep" placeholder="请输入奖励递进步长" size="small" clearable />
        </el-form-item>
        <el-form-item label="奖励值" prop="rewardvalue">
          <el-input v-model="form.rewardvalue" placeholder="请输入奖励值" size="small" clearable />
        </el-form-item>
        <el-form-item label="奖励封顶金额" prop="rewardvalue">
          <el-input v-model="form.maxrewards" placeholder="请输入奖励封顶金额" size="small" clearable />
        </el-form-item>
        <el-form-item label="惩罚标准比较类型" prop="punisheschecktype">
          <el-select v-model="form.punisheschecktype" placeholder="请选择惩罚标准比较类型" size="small" clearable>
            <el-option
              v-for="dict in punisheschecktypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="惩罚标准比较值" prop="punishescheckvalue">
          <el-input v-model="form.punishescheckvalue" placeholder="请输入惩罚标准比较值" size="small" clearable />
        </el-form-item>
        <el-form-item label="惩罚递进步长" prop="punishescheckstep">
          <el-input v-model="form.punishescheckstep" placeholder="请输入惩罚递进步长" size="small" clearable />
        </el-form-item>
        <el-form-item label="惩罚值" prop="punishesvalue">
          <el-input v-model="form.punishesvalue" placeholder="请输入惩罚值" size="small" clearable />
        </el-form-item>
        <el-form-item label="扣罚封顶金额" prop="punishesvalue">
          <el-input v-model="form.maxpunishes" placeholder="请输入扣罚封顶金额" size="small" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="maxacctval">
          <el-input v-model="form.remark" placeholder="请输入备注" size="small" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQaitems, getQaitems, delQaitems, addQaitems, updateQaitems, exportQaitems } from "@/api/quality/qaitems";
import { findQastandardList, findQamainList} from "@/api/public";

export default {
  name: "Qaitems",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质检项管理表格数据
      qaitemsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 质检项标识字典
      qaflagOptions: [],
      // 原料薯系列字典
      rawpotatosOptions: [],
      // 适用区域字典
      areaOptions: [],
      // 合格标准比较类型字典
      qualifiedchecktypeOptions: [],
      // 不合格比较类型字典
      unqualifiedchecktypeOptions: [],
      // 奖励标准比较类型字典
      rewardchecktypeOptions: [],
      // 惩罚标准比较类型字典
      punisheschecktypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemname: null,
        maxacctval: null,
        status: "0",
        listorder: null,
        qamainguid: null,
        qastandard: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemname: [
          { required: true, message: "抽检项名称不能为空", trigger: "blur" }
        ],
        listorder: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
      },
      standardOptions: [],
      mainOptions: [],
    };
  },
  created() {
    this.getList();
    this.findQastandardList();
    this.findQamainList();
    this.getDicts("qaitem_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("raw_potatoes").then(response => {
      this.rawpotatosOptions = response.data;
    });
    this.getDicts("qa_area").then(response => {
      this.areaOptions = response.data;
    });
    this.getDicts("check_type").then(response => {
      this.qualifiedchecktypeOptions = response.data;
    });
    this.getDicts("check_type").then(response => {
      this.unqualifiedchecktypeOptions = response.data;
    });
    this.getDicts("check_type").then(response => {
      this.rewardchecktypeOptions = response.data;
    });
    this.getDicts("check_type").then(response => {
      this.punisheschecktypeOptions = response.data;
    });
  },
  methods: {
    getStand(event,item){
      this.form.qastandardname = item.standard;
    },
    //口杂标准
    findQastandardList(){
      findQastandardList().then(response => {
        this.standardOptions = response.data;
      });
    },
    //质检标准
    findQamainList(){
      findQamainList({status:0}).then(response => {
        this.mainOptions = response.data;
      });
    },
    /** 查询质检项管理列表 */
    getList() {
      this.loading = true;
      listQaitems(this.queryParams).then(response => {
        this.qaitemsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 原料薯系列字典翻译
    rawpotatosFormat(row, column) {
      return this.selectDictLabel(this.rawpotatosOptions, row.rawpotatos);
    },
    // 适用区域字典翻译
    areaFormat(row, column) {
      return this.selectDictLabel(this.areaOptions, row.area);
    },
    // 合格标准比较类型字典翻译
    qualifiedchecktypeFormat(row, column) {
      return this.selectDictLabel(this.qualifiedchecktypeOptions, row.qualifiedchecktype);
    },
    // 不合格比较类型字典翻译
    unqualifiedchecktypeFormat(row, column) {
      return this.selectDictLabel(this.unqualifiedchecktypeOptions, row.unqualifiedchecktype);
    },
    // 奖励标准比较类型字典翻译
    rewardchecktypeFormat(row, column) {
      return this.selectDictLabel(this.rewardchecktypeOptions, row.rewardchecktype);
    },
    // 惩罚标准比较类型字典翻译
    punisheschecktypeFormat(row, column) {
      return this.selectDictLabel(this.punisheschecktypeOptions, row.punisheschecktype);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        itemname: null,
        status: 0,
        listorder: null,
        operatorid: null,
        createtime: null,
        lastupdate: null,
        qamainguid: null,
        qastandard: null,
        qastandardname: null,
        rawpotatos: null,
        area: null,
        qualifiedchecktype: null,
        qualifiedcheckvalue: null,
        unqualifiedchecktype: null,
        unqualifiedcheckvalue: null,
        rewardchecktype: null,
        rewardcheckvalue: null,
        rewardcheckstep: null,
        rewardvalue: null,
        punisheschecktype: null,
        punishescheckvalue: null,
        punishescheckstep: null,
        punishesvalue: null,
        maxrewards: null,
        maxpunishes: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加质检项管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids
      getQaitems(guid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改质检项管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != null) {
            updateQaitems(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQaitems(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delQaitems(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有质检项管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportQaitems(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
